#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/10  9:55
# @Author: 余浪人
# @email: yulangren520@gmail.com

from wtforms import Form, StringField, validators, PasswordField, RadioField, IntegerField
from flask import request
from apps.models.member_model import Member


class Member_Form(Form):
    email = StringField('邮箱', validators=[
        validators.DataRequired('邮箱不可为空!'),
        validators.Regexp(r'^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$', message="邮箱格式错误!")],
                        render_kw={"lay-verify": "email", "required": "", "class": "layui-input"})
    username = StringField(
        '用户名', validators=[validators.DataRequired('用户名不可为空!'), validators.Length(2, message='用户名长度不足!')],
        render_kw={"lay-verify": "nikename", "required": "", "class": "layui-input"})
    password = PasswordField(
        label=u'密码', validators=[validators.Length(min=6, message='密码至少6位!')],
        render_kw={"required": "", "class": "layui-input"})
    password2 = PasswordField(
        label=u'确认密码', validators=[validators.EqualTo('password', '两次密码不一致!')],
        render_kw={"required": "", "class": "layui-input"})

    def validate_email(self, email):
        cou = Member.query.filter(Member.email == email.data).count()
        if cou:
            raise validators.ValidationError("此邮箱已存在!")


class Member_Edit_Form(Form):
    email = StringField('邮箱', validators=[
        validators.DataRequired('邮箱不可为空!'),
        validators.Regexp(r'^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$', message="邮箱格式错误!")],
                        render_kw={"lay-verify": "email", "required": "", "class": "layui-input"})
    username = StringField(
        '用户名', validators=[
            validators.DataRequired('用户名不可为空!'), validators.Length(2, message='用户名长度不足!')],
        render_kw={"lay-verify": "nikename", "required": "", "class": "layui-input"})
    gender = RadioField('性别', validators=[
        validators.DataRequired('性别不可为空!')], choices=[(1, '保密'), (2, '男'), (3, '女')], default=1, coerce=int)
    qq = StringField('QQ', validators=[validators.DataRequired('QQ不可为空!'),
        validators.Regexp(r'^[1-9]\d{4,9}$', message='QQ号码格式错误!')], render_kw={"class": "layui-input"})
    credits = IntegerField('积分', validators=[
        validators.DataRequired('积分不可为空!')], render_kw={"class": "layui-input"})

    def validate_email(self, email):
        obj = Member.query.filter(Member.email == email.data)
        if obj.count() and (obj.all()[0].uuid !=request.args.get('uid')):   # 验证邮箱
            raise validators.ValidationError("此邮箱已存在!")


# 修改密码
class Member_Password_Edit_Fomr(Form):
    username = StringField('用户名', validators=[
            validators.DataRequired('用户名不可为空!'), validators.Length(2, message='用户名长度不足!')],
        render_kw={"disabled": "", "class": "layui-input"})
    password_old = PasswordField(label=u'密码',validators=[validators.DataRequired('请输入密码原密码')], render_kw={"required": "", "class": "layui-input"})
    password = PasswordField(
        label=u'密码', validators=[validators.DataRequired('请输入密码新密码!'),validators.Length(min=6, message='密码至少6位!')],render_kw={"class": "layui-input"})
    password2 = PasswordField(
        label=u'确认密码', validators=[validators.EqualTo('password', '两次密码不一致!')],
        render_kw={"class": "layui-input"})

# 积分
class Grade_Form(Form):
    grade_name =StringField('等级名',validators=[
        validators.DataRequired('等级名不可为空!'),validators.Length(2,message='等级名长度不足!')],
                            render_kw={"required": "","lay-verify":"required", "class": "layui-input"})
    grade_scope = StringField('积分',validators=[
        validators.DataRequired('积分不可为空!'),validators.Regexp(r'^\d+-\d+$',message='积分格式错误!')],
                              render_kw={"required": "","lay-verify":"required", "class": "layui-input"})